package com.hcb.tonguetiphealth.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.hcb.tonguetiphealth.manager.AsyncBitmapLoader;
import com.hcb.tonguetiphealth.manager.AsyncBitmapLoader.ImageCallBack;
import com.hcb.tonguetiphealth.model.ResDish;
import com.hcb.tonguetiphealth.model.Restaurant;
import com.hcb.tonguetiphealth.view.R;

public class DishListAdapter extends BaseAdapter {

	private Context mContext;
	private List<ResDish> mList;
	private LayoutInflater vi;
	private AsyncBitmapLoader asyncBitmapLoader;
	
	public DishListAdapter(Context context,List<ResDish> list) {
		mContext = context;
		mList = list;
		asyncBitmapLoader = AsyncBitmapLoader.getInstance();
	}
	
	public int getCount() {
		if(mList == null) {
			return 0;
		}
		return mList.size();
	}
	
	public Object getItem(int position) {
		if(mList == null) {
			return null;
		} else {
			return mList.get(position);
		}
	}
	
	public long getItemId(int position) {
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {

		View view;
		
		if(convertView == null) {
			vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = (LinearLayout) vi.inflate(R.layout.online_book_dish_list_item, null);
		} else {
			view = convertView;
		}
		FillDataIntoView(view, position);
		
		return view;
	}
	
	private void FillDataIntoView(View view, int position) {
		final ResDish dish = mList.get(position);

		ImageView img = (ImageView)view.findViewById(R.id.dish_img);
		TextView tv_name = (TextView)view.findViewById(R.id.dish_name_textview);
		TextView tv_books = (TextView)view.findViewById(R.id.dish_books_textview);
		TextView tv_price = (TextView)view.findViewById(R.id.dish_bookprice_textview );
		//img.setBackgroundResource(R.drawable.menu_entrance);
		tv_name.setText(dish.getTitle());
		tv_books.setText("null".equals(dish.getNum())?"":dish.getNum());
		tv_price.setText(dish.getPrice()+ " 元");
		
		String imageURL = dish.getDishThumb(); 
        Bitmap bitmap = asyncBitmapLoader.loadBitmap(img, imageURL, new ImageCallBack() {  
              
            public void imageLoad(ImageView imageView, Bitmap bitmap) {  
                // TODO Auto-generated method stub  
                imageView.setImageBitmap(bitmap);  
            }  
        });  
		if(bitmap == null) {    
			img.setImageResource(R.drawable.ic_launcher);    
		} else {    
		    img.setImageBitmap(bitmap);    
		}
	    
	}
}
